package com.liulei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liulei.entities.Customer;
import com.liulei.mapper.CustomerMapper;
import com.liulei.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 客户管理接口实现类
 */

@Service
public class CustomerServiceImpl implements CustomerService {

    //自动装配客户管理Mapper接口
    @Autowired
    private CustomerMapper customerMapper;
    /**
     * 返回所有客户信息
     *
     * @return 客户列表
     */

    @Override
    public List<Customer> listCustomer() {
        return customerMapper.selectList(null);
    }

    /**
     * 添加客户信息
     * @param customer 表单采集的客户信息
     * @return 受影响行数，大于0添加成功，否则添加失败
     */

    @Override
    public int saveCustomer(Customer customer) {
        return customerMapper.insert(customer);
    }

    /**
     * 根据客户名称搜索满足条件客户信息
     *
     * @param custName 客户名称
     * @return 客户列表
     */

    @Override
    public List<Customer> searchCustomer(String custName) {
        QueryWrapper<Customer> qw = new QueryWrapper<>();
        qw.like("cust_name",custName);
        return customerMapper.selectList(qw);
    }

    /**
     * 根据客户ID删除对应的客户信息
     *
     * @param cid 客户ID
     * @return 受影响行数，大于0删除成功，否则删除失败
     */

    @Override
    public int deleteCustomerById(Integer cid) {
        QueryWrapper<Customer> qw = new QueryWrapper<>();
        qw.eq("cid",cid);
        return customerMapper.delete(qw);
    }

    /**
     * 根据客户ID查询客户信息
     *      * @param cid 客户id
     *      * @return 客户实体
     *
     * @param cid
     * @return
     */
    @Override
    public Customer getCustomerById(Integer cid) {
        QueryWrapper<Customer> qw = new QueryWrapper<>();
        qw.eq("cid",cid);
        return customerMapper.selectOne(qw);
    }

    @Override
    public int updateCustomer(Customer customer) {
        QueryWrapper<Customer> qw = new QueryWrapper<>();
        qw.eq("cid",customer.getCid());
        return customerMapper.update(customer,qw);
    }
}
